package com.example.notificationservice.service.Impl;

import com.example.notificationservice.entity.NotificationEntity;
import com.example.notificationservice.repository.NotificationRepository;
import com.example.notificationservice.service.NotificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * @Author: dang
 * @Description: TODO
 * @Date: 2025/4/15 17:14 星期二
 */
@Service
public class NotificationServiceImpl implements NotificationService {
    @Autowired
    private NotificationRepository notificationRepository;

    @Override
    public NotificationEntity createNotification(NotificationEntity notification) {
        notification.setCreatedAt(new Date());
        return notificationRepository.save(notification);
    }

    @Override
    public NotificationEntity getNotificationById(String notificationId) {
        return notificationRepository.findById(notificationId).orElse(null);
    }

    @Override
    public NotificationEntity markNotificationAsRead(String notificationId) {
        NotificationEntity notification = notificationRepository.findById(notificationId).orElse(null);
        if (notification != null) {
            notification.setRead(true);
            return notificationRepository.save(notification);
        }
        return null;
    }

    @Override
    public void deleteNotification(String notificationId) {
        notificationRepository.deleteById(notificationId);
    }

    @Override
    public List<NotificationEntity> getAllNotifications() {
        return notificationRepository.findAll();
    }

    @Override
    public List<NotificationEntity> getNotificationsByCustomerId(String customerId) {
        return notificationRepository.findByCustomerId(customerId);
    }
    @Override
    public List<NotificationEntity> getNotificationsByType(String type) {
        return notificationRepository.findByType(type);
    }

    @Override
    public List<NotificationEntity> getNotificationsByCustomerIdAndType(String customerId, String type) {
        return notificationRepository.findByCustomerIdAndType(customerId, type);
    }

    @Override
    public List<NotificationEntity> getUnreadNotificationsByCustomerId(String customerId) {
        return notificationRepository.findByCustomerIdAndRead(customerId, false);
    }
}
